package com.bins.dentist.clinic.code.generate;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

public class CodeGenerator {

    private static final String JDBC_URL = "jdbc:mysql://118.31.247.14:3306/dentist_clinic?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSl=true";
    private static final String JDBC_USER_NAME = "root";
    private static final String JDBC_PASSWORD = "123456";

    // 包名和模块名
    private static final String PACKAGE_NAME = "com.bins.dentist.clinic";
    private static final String MODULE_NAME = "goods";

    // 表名,多个表使用英文逗号分割
    private static final String[] TBL_NAMES = {"t_dept", "t_menu", "t_permission", "t_role", "t_role_permission", "t_user", "t_user_group", "t_user_role"};

    // 表名的前缀,从表生成代码时会去掉前缀
    private static final String TABLE_PREFIX = "t_";


    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(JDBC_URL, JDBC_USER_NAME, JDBC_PASSWORD)
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());
        FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder);
        fastAutoGenerator.globalConfig(
                globalConfigBuilder -> globalConfigBuilder
                        .fileOverride()
                        .disableOpenDir()
                        .outputDir(projectPath + "/src/main/java")
                        .author("bins")
                        .commentDate("yyyy-MM-dd HH:mm:ss")
                        .dateType(DateType.TIME_PACK)
                        .enableSwagger()
        );

        fastAutoGenerator.packageConfig(
                packageConfigBuilder -> packageConfigBuilder
                        .parent(PACKAGE_NAME)
                        // .moduleName(MODULE_NAME)
                        .entity("model.po")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .controller("controller")
                        .xml("mapper.xml")
        );

        /*
        fastAutoGenerator.templateConfig(
                templateConfigBuilder -> templateConfigBuilder
                        .disable(TemplateType.ENTITY)
                        .entity("/templates/entity.java")
                        .service("/templates/service.java")
                        .serviceImpl("/templates/serviceImpl.java")
                        .mapper("/templates/mapper.java")
                        .xml("/templates/mapper.xml")
                        .controller("/templates/controller.java")
        );
        */

        fastAutoGenerator.strategyConfig(
                strategyConfigBuilder -> strategyConfigBuilder
                        .enableCapitalMode()
                        .enableSkipView()
                        .disableSqlFilter()
                        .addInclude(TBL_NAMES)
                        .addTablePrefix(TABLE_PREFIX)
        );

        fastAutoGenerator.strategyConfig(
                strategyConfigBuilder -> strategyConfigBuilder.entityBuilder()
                        .enableTableFieldAnnotation()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .idType(IdType.ASSIGN_ID.AUTO)
                        .enableLombok()
                        // .logicDeleteColumnName("deleted")
                        // .logicDeletePropertyName("deleted")
                        // .addTableFills(new Column("create_time", FieldFill.INSERT))
                        // .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        // .versionColumnName("version")   // 开启乐观锁
                        .disableSerialVersionUID()
                        .enableChainModel()
                        .formatFileName("%sEntity")
        );

        fastAutoGenerator.strategyConfig(
                strategyConfigBuilder -> strategyConfigBuilder.controllerBuilder().enableHyphenStyle()
                        .enableRestStyle()
                        .enableHyphenStyle()
        );


        fastAutoGenerator.strategyConfig(
                strategyConfigBuilder -> strategyConfigBuilder.serviceBuilder()
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl"));

        fastAutoGenerator.strategyConfig(
                strategyConfigBuilder -> strategyConfigBuilder.mapperBuilder()
                        .enableMapperAnnotation()
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper"));

        // fastAutoGenerator.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
        fastAutoGenerator.execute();
    }
}
